home *** CD-ROM | disk | FTP | other *** search
- Path: newshost.lanl.gov!tanmoy
- From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
- Newsgroups: comp.lang.c
- Subject: Re: accessing structures (newbie question)
- Date: 19 Feb 1996 16:08:45 GMT
- Organization: Los Alamos National Laboratory
- Message-ID: <TANMOY.96Feb19090845@qcd.lanl.gov>
- References: <4g8gic$o6u@news1.sunbelt.net> <3128FA60.5227@metagen.co.uk>
- NNTP-Posting-Host: qcd.lanl.gov
- Mime-Version: 1.0
- Content-Type: text
- In-reply-to: Rob Stenton's message of Mon, 19 Feb 1996 14:32:00 -0800
-
- In article <3128FA60.5227@metagen.co.uk> Rob Stenton
- <rws@metagen.co.uk> writes:
- <snip>
- There are basically two ways of accessing elements within a malloced
- buffer:
- 1. Treat the malloced buffer as an array (as you suggested).
- 2. Increment a local pointer by sizeof(picture) in loop iteration.
-
- The first method is preferable because the second method involves pointer
- arithmetic (always best avoided) which can cause problems on different
- memory architectures and assumes things about BYTE sizes (but usually
- works).
-
- What nonsense! Anything creates trouble if you do not take the time
- to understand it.
-
- I think you may have been using the wrong syntax when you tried the array
- method. The xth item is given by 'photos[x].' which is equivalent to
- '(photos + x * sizeof(picture))->'.
-
- This is the problem!!! photos[x]. is always the same as (photos + x)->
- In fact, this is such a basic fact of pointer arithmetic that anyone
- who posts such nonsense must surely be trying to deliberately mislead
- newbies. We have enough confusion here: please refrain from posting
- _answers_ unless you know at least the basics of C!
-
- When an integer x is added to a pointer photos to a struct, the
- pointer is updated so as to skip over x structs, not x bytes
- as this poster maliciously suggested.
-
- Cheers
- Tanmoy
- --
- tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
- Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
- Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
- <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
- internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
- fax: 1 (505) 665 3003 voice: 1 (505) 665 4733 [ Home: 1 (505) 662 5596 ]
-